package com.desarrolladoresandroid.libgdx.screen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.desarrolladoresandroid.libgdx.demo1.Assets;
import com.desarrolladoresandroid.libgdx.demo1.Demo1Game;
import com.desarrolladoresandroid.libgdx.models.Ball;
import com.desarrolladoresandroid.libgdx.models.Bar;
import com.desarrolladoresandroid.libgdx.models.Lad;
import com.desarrolladoresandroid.libgdx.models.World;

public class GameScreen implements Screen{

	Game game;
	
	OrthographicCamera guiCam;
	SpriteBatch batcher;
	
	Vector3 touchPoint;
	
	World world;
	
	public GameScreen(Game game){
		this.game = game;
		guiCam= new OrthographicCamera(10,15); //ancho y alto de la camara
		guiCam.position.set(10f / 2, 15f / 2, 0);
		batcher = new SpriteBatch();
		
		touchPoint = new Vector3();
		
		world = new World();
	
		
	}

	@Override
	public void dispose() {
		batcher.dispose();
		
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void pause() {		
		
	}

	@Override
	public void render(float delta) {
		
		if (Gdx.input.isTouched()){
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(),0));
			world.bar.setPos(touchPoint.x - 1);
		}
		
		
		world.update(delta); //recibe el tiempo transcurrido entre update y update
		
		if(world.isFinished){
			game.setScreen(((Demo1Game)game).mainMenu);
			return;
		}
		
		GL10 gl = Gdx.graphics.getGL10();
		gl.glClearColor(0,1,0,1);          //establecemos el color con el que queremos borrar la pantalla
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);
		
		batcher.disableBlending();
		batcher.begin();
		batcher.draw(Assets.background,0,0,10,15);
		batcher.draw(Assets.vertical,0,0,1,15);
		batcher.draw(Assets.vertical,9,0,1,15);
		batcher.draw(Assets.horizontal, 0,14,10,1);
		//Barra
		Bar bar = world.bar;
		batcher.draw(Assets.bar, bar.position.x, bar.position.y - 0.5f, 2f, 0.5f);
		batcher.end();

		//ball
		batcher.enableBlending();
		batcher.begin();
		Ball ball = world.ball;
		batcher.draw(Assets.ball, ball.position.x - 1f/8, ball.position.y - 1f/8, 0.25f, 0.25f);

		batcher.end();
		
		//ladrillos
		Lad lad =  world.lad;
		if(lad.eliminado==0){
			batcher.enableBlending();
			batcher.begin();
			batcher.draw(Assets.bar, lad.position.x, lad.position.y - 0.5f, 2f, 0.5f);
			batcher.end();
		}
		
	}

	@Override
	public void resize(int arg0, int arg1) {
		
		
	}

	@Override
	public void resume() {
	
		
	}

	@Override
	public void show() {
		
		
	}

}
